package com.ly.mp.cmc.common.reponse;


import org.hibernate.validator.internal.util.StringHelper;

import java.util.Collection;

/**
 * @Author: ly-songpengfei
 * @Date: 2025/8/3 18:23
 * @Description: 返回结果工具类
 * @Version: 1.0
 */
public class ResultUtil<T> {

    public static BaseResult success() {
        BaseResult baseResult = new BaseResult();
        return baseResult;
    }

    public static BaseResult success(ResultEnum resultEnum) {
        BaseResult baseResult = new BaseResult(resultEnum.getCode(), resultEnum.getMsg());
        return baseResult;
    }

    public static <T> Result<T> success(T entity, String bizInfo) {
        Result<T> result = new Result();
        result.setCode(ResultEnum.QUERY_SUCCESS.getCode());
        result.setMsg(ResultEnum.QUERY_SUCCESS.getMsg());
        result.setRows(entity);
        if (entity instanceof Collection) {
            result.setRecords(((Collection<?>) entity).size());
        }
        if (StringHelper.isNullOrEmptyString(bizInfo)) {
            result.setBizInfo(bizInfo);
        }
        return result;
    }

    public static BaseResult error() {
        BaseResult baseResult = new BaseResult(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
        return baseResult;
    }

    public static BaseResult error(ResultEnum resultEnum) {
        BaseResult baseResult = new BaseResult(resultEnum.getCode(), resultEnum.getMsg());
        return baseResult;
    }

}
